package utils;

import irio.gamer.gamer;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JVector extends JPanel{

	private Vec vec = null;
	private Vec from = null;
	private Vec to = null;

	public boolean normalized = false;
	
	public boolean postprocessing = false;
	
	JSlider x,y,z;
	JFormattedTextField tx, ty, tz;
	private static final float val = 1000;
	private NumberFormat df;
	

	
	public JVector(String name, Vec v, Vec f, Vec t, NumberFormat format) {
		Create(name,v,f,t,format);
	}
	public JVector(String name, Vec v, Vec f, Vec t, NumberFormat format, String s) {
		Create(name,v,f,t,format);
		setToolTipText(s);
	}
	
	
	private void Create(String name, Vec v, Vec f, Vec t, NumberFormat format) {
		df = format;
		vec = v;
		from = f;
		to = t;

		setLayout(new GridLayout(4,2));
		setBorder(BorderFactory.createLineBorder(Color.gray));

		
		
		setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10, 10, 10, 10), new EtchedBorder()));
		add(new JLabel(name));
		add(new JLabel(""));
		NumberFormat ff = NumberFormat.getNumberInstance(); 
		ff.setMaximumIntegerDigits(4 );
		tx = new JFormattedTextField(ff);
		add(tx);
		tx.setText(df.format(v.x));
		tx.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				vec.x = Double.parseDouble((tx.getValue().toString()));
				x.setValue((int)(vec.x*val));
				
			}
		});
		
		x = new JSlider(JSlider.HORIZONTAL,
                (int)(from.x*val), (int)(to.x*val), (int)(vec.x*val));
		add(x);
		x.setPaintTicks(true);
		x.addChangeListener(new ChangeListener() {
			
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				JSlider source = (JSlider)e.getSource();
			    //if (!source.getValueIsAdjusting()) 
			    {
			        vec.x = (float)(source.getValue()/val);
			        tx.setText(df.format(vec.x));
			        if (gamer.imagePanel!=null)

			        gamer.imagePanel.preview.resetPreview(postprocessing);
			    }
			}
		});


		/*
		 *  Y
		 */

		ty = new JFormattedTextField(ff);
		add(ty);
		ty.setText(df.format(v.y));
		ty.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				vec.y = Double.parseDouble((ty.getValue().toString()));
				y.setValue((int)(vec.y*val));
			}
		});
	
		y = new JSlider(JSlider.HORIZONTAL,
            (int)(from.y*val), (int)(to.y*val), (int)(vec.y*val));
		add(y);
		y.setPaintTicks(true);
		y.addChangeListener(new ChangeListener() {
		
		public void stateChanged(ChangeEvent e) {
			// TODO Auto-generated method stub
			JSlider source = (JSlider)e.getSource();
		    //if (!source.getValueIsAdjusting()) 
			{
		        vec.y = (float)(source.getValue()/val);
		        if (gamer.imagePanel != null) 
		        	gamer.imagePanel.preview.resetPreview(postprocessing);
		        ty.setText(df.format(vec.y));
		    }
		}
	});

		tz = new JFormattedTextField(ff);
		add(tz);
		tz.setText(df.format(v.z));
		tz.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				vec.z = Double.parseDouble((tz.getValue().toString()));
				z.setValue((int)(vec.z*val));
			}
		});
	
	z = new JSlider(JSlider.HORIZONTAL,
        (int)(from.z*val), (int)(to.z*val), (int)(vec.z*val));
	add(z);
	z.setPaintTicks(true);
	z.addChangeListener(new ChangeListener() {
	
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		JSlider source = (JSlider)e.getSource();
	    //if (!source.getValueIsAdjusting())
		{
	        vec.z = (float)(source.getValue()/val);
	        if (gamer.imagePanel!=null)
	        gamer.imagePanel.preview.resetPreview(postprocessing);
	        tz.setText(df.format(vec.z));

	    }
	}
});



	tx.setSize(50,50);

	}
	
	
}
